const { console } = require("inspector");


/**
 * 连接小狐狸钱包
 * @returns {Promise<Page>}
 */

const follow = async (browser, page) => {
    const followed = await page.locator("xpath=//div[text()='Follow us on X']/../../../div[2]/div/img").all()
    if (followed.length > 0){
        console.log('已关注')
        return;
    }
    const [newPage] = await Promise.all([
        browser.waitForEvent('page', {timeout:24000}), // 等待新窗口打开
        page.locator("xpath=//button[text()='Follow']").click() // 点击触发新窗口的按钮
        ])
    await newPage.waitForTimeout(2000); // 等待2秒
    await newPage.locator('xpath=//button//span[contains(text(), "Follow ")]').click()
    console.log('已关注')
    await newPage.close()
    await page.locator("xpath=//button[text()='Follow']/following-sibling::*[1]").click()
    const end = "" + Date.now();
    console.log(end + "完成关注操作!")
}
const retweet = async (browser, page) => {
    const start = "" + Date.now();
    console.log(start + '开始执行转发操作!')
    const [newPage] = await Promise.all([
        browser.waitForEvent('page', {timeout:24000}), // 等待新窗口打开
        page.locator("xpath=//button[text()='Retweet']").click() // 点击触发新窗口的按钮
        ])
    await newPage.waitForTimeout(2000); // 等待2秒
    await newPage.locator('xpath=//button//span[contains(text(), "Repost")]').click()
    console.log('已关注')
    await newPage.close()
    await page.locator("xpath=//button[text()='Retweet']/following-sibling::*[1]").click()
    const end = "" + Date.now();
    console.log(end + "完成转发操作!")
}

const reply = async (browser, page) => {
    const start = "" + Date.now();
    console.log(start + '开始执行回复操作!')
    const [newPage] = await Promise.all([
        browser.waitForEvent('page', {timeout:24000}), // 等待新窗口打开
        page.locator("xpath=//button[text()='Reply']").click() // 点击触发新窗口的按钮
        ])
    await newPage.waitForTimeout(2000); // 等待2秒
    await newPage.locator("xpath=//div[2]/div[2]//div[@role='textbox']").fill('666')
    await newPage.locator('xpath=//button//span[contains(text(), "Reply")]').click()
    console.log('已回复')
    await newPage.close()
    await page.locator("xpath=//button[text()='Reply']/following-sibling::*[1]").click()
    const end = "" + Date.now();
    console.log(end + "完成回复操作!")
}

const addTestnet = async (browser, page) => {
    console.log('领取测试币')
    await page.locator("xpath=//li[text()='Home']").click()
    await page.locator("xpath=//button[text()='CLAIM NOW']").scrollIntoViewIfNeeded();
    await page.waitForTimeout(2000); // 等待2秒
    const frame = page.frameLocator('xpath=//*[@id="root"]//div[3]/div[1]/div[3]/div/div/div//iframe')
    await frame.locator("#recaptcha-anchor").click()
}



module.exports = { connectWallet,checked, follow, retweet, reply, addTestnet };
